package guice.boot.archive.container.registry;

import com.sun.istack.internal.NotNull;
import guice.boot.archive.spi.model.Plugin;
import guice.boot.archive.spi.registry.ServiceProviderType;

/**
 * Plugin Service Provider, when service is published by plugin, then use this provider
 */
public class PluginServiceProvider extends AbstractServiceProvider {

    private Plugin plugin;

    public PluginServiceProvider(@NotNull Plugin plugin) {
        super(ServiceProviderType.archive_plugin);
        this.plugin = plugin;
    }

    @Override
    public String getServiceProviderDesc() {
        return String.format("%s:%s", super.getServiceProviderDesc(), plugin.getName());
    }

    @Override
    public int getPriority() {
        return plugin.getPriority();
    }

    public String getPluginName() {
        return plugin.getName();
    }

    public Plugin getPlugin() {
        return plugin;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + plugin.hashCode();
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (!super.equals(obj)) {
            return false;
        }

        PluginServiceProvider pluginServiceProvider = (PluginServiceProvider) obj;

        return plugin.equals(pluginServiceProvider.getPlugin());
    }
}